package com.lxp.modules.gyy.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxp.common.core.domain.AjaxResult;
import com.lxp.common.core.page.TableDataInfo;
import com.lxp.modules.gyy.domain.ApprovalApprover;
import com.lxp.modules.gyy.domain.ApprovalRequest;
import com.lxp.modules.gyy.domain.IrAttachment;
import com.lxp.modules.gyy.domain.vo.AuditParam;
import com.lxp.modules.gyy.mapper.ApprovalApproverMapper;
import com.lxp.modules.gyy.mapper.ApprovalRequestMapper;
import com.lxp.modules.gyy.mapper.IrAttachmentMapper;
import com.lxp.modules.gyy.service.ApprovalRequestService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【approval_request(Approval Request)】的数据库操作Service实现
 * @createDate 2024-04-25 10:45:16
 */
@Service
public class ApprovalRequestServiceImpl extends ServiceImpl<ApprovalRequestMapper, ApprovalRequest> implements ApprovalRequestService {

    @Resource
    private IrAttachmentMapper attachmentMapper;
    @Resource
    private ApprovalApproverMapper approverMapper;

    @Override
    public TableDataInfo pageList(AuditParam param, TableDataInfo info) {
        int pageNum = info.getPageNum() - 1;
        Integer pageSize = info.getPageSize();
        int start = PageUtil.getStart(pageNum, pageSize);
        int endPage = PageUtil.getEnd(pageNum, pageSize);
        Date date = new Date();
        if (param.getStartDate() == null) {
            Date jdkDate = DateUtil.offsetHour(DateUtil.beginOfMonth(date).toJdkDate(), -8).toJdkDate();
            param.setStartDate(jdkDate);
        }
        if (param.getEndDate() == null) {
            Date jdkDate = DateUtil.offsetHour(DateUtil.endOfMonth(date).toJdkDate(), -8).toJdkDate();
            param.setEndDate(jdkDate);
        }
        List<ApprovalRequest> auditList = this.baseMapper.pageList(param);
        info.setTotal(auditList.size());
        if (auditList.size() > endPage) {
            auditList = auditList.subList(start, endPage);
        } else {
            auditList = auditList.subList(start, auditList.size());
        }
        info.setRows(auditList);
        return info;
    }

    @Override
    public AjaxResult getOneById(Long approvalId) {
        ApprovalRequest byId = this.baseMapper.selectOneById(approvalId);

        LambdaQueryWrapper<IrAttachment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(IrAttachment::getResId, approvalId);
        queryWrapper.eq(IrAttachment::getResModel, "approval.request");
        List<IrAttachment> list = attachmentMapper.selectList(queryWrapper);

        List<ApprovalApprover> approverList = approverMapper.getListByRequest(byId.getId());
        byId.setApproverList(approverList);
        byId.setAttachmentList(list);
        return AjaxResult.success(byId);
    }
}




